package com.shadow.cloud.data;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis操作
 * */
@Component
public class RedisCacheHelper {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * set -> set [key] [value]
     *
     * @param key   key
     * @param value value
     */
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * get -> get [key]
     *
     * @param key key
     */
    public String get(String key) {
        if (StringUtils.isNotBlank(key)) {
            Object o = redisTemplate.opsForValue().get(key);
            if (null != o) return o.toString();
        }
        return null;
    }

    /**
     * del -> del [key]
     *
     * @param key key
     */
    public Boolean del(String key) {
        Boolean deleted = Boolean.FALSE;
        if (StringUtils.isNotBlank(key)) {
            deleted = redisTemplate.delete(key);
        }
        return deleted;
    }

    /**
     * hSet -> hSet [key] [field] [value]
     *
     * @param key   key
     * @param field field
     * @param value value
     * @param expire 秒 @NotNull
     * */
    public int hSet(final String key, final String field, final String value, final long expire) {
        if (StringUtils.isNotBlank(key) && 0L != expire) {
            redisTemplate.opsForHash().put(key, field, value);
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
            return 1;
        }
        return 0;
    }

    /**
     * hGet -> hGet [key] [field]
     *
     * @param key   key
     * @param field field
     * */
    public String hGet(final String key, final String field) {
        String string = null;
        if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(field)) {
            string = (String) redisTemplate.opsForHash().get(key, field);
        }
        return string;
    }

    /**
     * hDel -> hDel [key] [field]
     *
     * @param key   key
     * @param field field
     */
    public long hDel(String key, String field) {
        long deleted = 0L;
        if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(field)) {
            deleted = redisTemplate.opsForHash().delete(key, field);
        }
        return deleted;
    }

    /**
     * hExists -> hExists [key] [field]
     *
     * @param key   key
     * @param field field
     */
    public Boolean hExists(String key, String field) {
        Boolean exist = false;
        if (StringUtils.isNotBlank(key)) {
            exist = redisTemplate.opsForHash().hasKey(key, field);
        }
        return exist;
    }

    /**
     * hLen -> hLen [key]
     *
     * @param key key
     */
    public long hLen(String key) {
        long size  = 0L;
        if (StringUtils.isNotBlank(key)) {
            size = redisTemplate.opsForHash().size(key);
        }
        return size;
    }

    /**
     * hKeys -> hKeys [key]
     *
     * @param key key
     */
    public Set<Object> hKeys(String key) {
        Set<Object> keys = null;
        if (StringUtils.isNotBlank(key)) {
            keys = redisTemplate.opsForHash().keys(key);
        }
        return keys;
    }
}
